package com.demo2do.fms.admin.web.controller.supplier.info;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.export.supplier.DriverSheet;
import com.demo2do.fms.admin.entity.search.supplier.DriverSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.DriverService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.driver.Driver;

/**
 * 司机信息查询
 * 
 * 业务逻辑：查询供方自己的司机信息
 * 		非供方 - 查询所有
 * 		供方 - 查询自己公司下司机
 */
@Controller
@RequestMapping(value = "/qualified-drivers")
@MenuSetting("menu-supplier")
public class QualifiedDriverController {
    
    @Autowired
    private DriverService driverService;
    
    /**
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-qualified-driver-management")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/info/driver/qualified-driver-info-list");
        modelAndView.addObject("supplier", login.isSupplier());

        int totals = driverService.count(login);
        if (totals > 0) {

            List<Driver> drivers = driverService.list(login, page.with(totals));

            modelAndView.addObject("drivers", drivers);
            modelAndView.addObject("page", page);
            
        }

        return modelAndView;
        
    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("supplier/info/driver/qualified-driver-info-search");
        return modelAndView;
    }

    /**
     * 
     * @param login
     * @param driverSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-qualified-driver-management")
    public ModelAndView onSearch(@Secure Login login, DriverSearch driverSearch, 
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/info/driver/qualified-driver-info-list");
        modelAndView.addObject("supplier", login.isSupplier());
        
        if (login.isSupplier()) {
            driverSearch.setSupplierId(login.getSupplierId());
        }
        
        int totals = driverService.count(driverSearch);
        if (totals > 0) {
            
            List<Driver> drivers = driverService.list(driverSearch, page.with(totals));
            
            modelAndView.addObject("drivers", drivers);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }

        return modelAndView;
        
    }

    /**
     * view 详情
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-qualified-driver-management")
    public ModelAndView view(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("supplier/info/driver/qualified-driver-info-view");

        return modelAndView;
    }
    
    /**
     * 导出自己的司机
     * 
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("supplier/info/driver/qualified-driver-info-export");
        return modelAndView;
    }
    
    /**
     * 导出自己的司机
     * 
     * @param search
     * @param login
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public ModelAndView onExport(DriverSearch search, @Secure Login login) {
    	
    	ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("supplier/driver-export.xlsx"));

    	// 目前此功能只有供方用户才能看到
    	if (login.isSupplier()) {
            search.setSupplierId(login.getSupplierId());
        }
    	
    	List<DriverSheet> driverSheets = driverService.export(search);
    	modelAndView.addObject("drivers", driverSheets);
    	
    	return modelAndView;
    	
    }

}
